<?php


namespace app\index\controller;

use think\Db;
use think\exception\DbException;

/**
 * 数据库操作field
 * Class DbFieldTest
 * @package app\index\controller
 */
class DbFieldTest
{
    /**
     * field
     * sql：SELECT `id`,`name`,`age` FROM `t_user`
     * @return \think\response\Json
     */
    public function field_test()
    {
        try {
            $res = Db::table('t_user')
                ->field('id,name,age')
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

    /**
     * field 别名
     * sql：SELECT `id`,name uName,`age` FROM `t_user`
     * @return \think\response\Json
     */
    public function field_test2()
    {
        try {
            $res = Db::table('t_user')
                ->field('id,name uName,age')
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

    /**
     * field 直接使用函数
     * sql：SELECT id,name uName,SUM(age) tAge FROM `t_user`
     * @return \think\response\Json
     */
    public function field_test3()
    {
        try {
            $res = Db::table('t_user')
                ->field('id,name uName,SUM(age) tAge')
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

    /**
     * field raw (函数推荐使用)
     * sql：SELECT id,name uName,SUM(age) tAge FROM `t_user`
     * @return \think\response\Json
     */
    public function field_raw_test()
    {
        try {
            $res = Db::table('t_user')
                ->fieldRaw('id,name uName,SUM(age) tAge')
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

    /**
     * field数组
     * @return \think\response\Json
     */
    public function field_arr_test()
    {
        try {
            $res = Db::table('t_user')
                ->field(['id', 'name', 'age'])
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

    /**
     * field 显式查找所有字段
     * sql：SELECT `id`,`name`,`age`,`status`,`delete_time` FROM `t_user`
     * @return \think\response\Json
     */
    public function field_all_test()
    {
        try {
            $res = Db::table('t_user')
                ->field(true)
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

    /**
     * field 隐式查找所有字段
     * sql：SELECT * FROM `t_user`
     * @return \think\response\Json
     */
    public function field_all_test1()
    {
        try {
            $res = Db::table('t_user')
                ->field('*')
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

    /**
     * 排除字段
     * sql：SELECT `id`,`name`,`age` FROM `t_user`
     * @return \think\response\Json
     */
    public function field_exclude_test()
    {
        try {
            $res = Db::table('t_user')
                ->field('status,delete_time', true)
                ->select();
            return json(['msg' => '查询成功！', 'data' => $res]);
        } catch (DbException $e) {
            return json(['msg' => '查询失败！']);
        }
    }

}